package com.matt.controller.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.constant.CommonConstant;
import com.matt.foundation.dto.AreaSettingDTO;
import com.matt.foundation.ext.aliyun.OSSProcessService;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.query.NurseryScheduleCheckQuery;
import com.matt.foundation.utils.annotation.RequiredPermissions;
import com.matt.foundation.vo.nursery.NurseryScheduleCheckVo;
import com.matt.service.CourseAuditService;
import com.matt.service.NurseryScheduleService;
import com.matt.service.PrepareCourseService;
import com.matt.service.UploadService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.matt.foundation.constant.PermissionConstant.TEACH_NURSERY_COURSEAUDIT_ACTION;
import static com.matt.foundation.constant.PermissionConstant.TEACH_NURSERY_COURSEAUDIT_SETTING;
import static com.matt.foundation.constant.RequestConstant.TICKET;

;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/11/13
 * *******************************
 */
@RestController
@RequestMapping("/web/course_audit")
public class WebCourseAuditController {

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private CourseAuditService courseAuditService;

    @Autowired
    private PrepareCourseService prepareCourseService;

    @Autowired
    private NurseryScheduleService nurseryScheduleService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private OSSProcessService ossProcessService;

    @Autowired
    private MemVerificationCache cache;

//    @Value("${aliyun.oss.fileServerPrefix}")
//    private String IMG_SERVICE_PREFIX;
//
//    @Value("${spring.profiles.active}")
//    private String env;

//    @Value("${pc.internet.domain.url}")
//    private String pcDomainUrl;

//    @Autowired
//    CourseTemplate1Repository courseTemplate1Repository;

    @ApiOperation(value = "获取园区设置",httpMethod = "POST")
    @RequestMapping("/get_auditor_setting")

    public StatusResponse auditSetting(@RequestHeader(TICKET) String ticket, String areaId) {
        Map map = courseAuditService.getAuditor(areaId);
        return StatusResponse.success(map);
    }

//    @ApiOperation(value = "标签设置",httpMethod = "POST")
//    @RequestMapping("/tag_setting")
//    public StatusResponse getAuditSetting(String areaId) {
//        Map map = courseAuditService.getAuditorSetting(areaId);
//        return StatusResponse.success(map);
//    }

    @ApiOperation(value = "修改园区设置",httpMethod = "POST")
    @RequestMapping("/add_auditor")
    @RequiredPermissions(TEACH_NURSERY_COURSEAUDIT_SETTING)
    public StatusResponse editAuditSetting(String areaId, String teacherId, Integer isOpen, Integer category) {
        Boolean result = courseAuditService.addAuditor(areaId, teacherId, isOpen, category);
        return StatusResponse.success(result);
    }

//    @ApiOperation(value = "备课审核列表",httpMethod = "POST")
//    @RequestMapping("/course_list")
//    @RequiredPermissions(TEACH_NURSERY_COURSEAUDIT_ACTION)
//    public StatusResponse courseList(@RequestHeader(TICKET) String ticket,ClassesCoursePlanQuery query) {
//        query.setCourseStatus(CourseConstant.CourseStatus.STARTING.codeOf());
//        StaffDTO baseInfo = cache.getStaff(ticket);
//        String areaId = cache.getAreaId(ticket);
//
////    	 List<StaffAreaDTO> staffList = ssoCallService.getStaffByPlatformAndRoleAndArea(CommonConstant.TEACH_PLATFORM_ID, new String[]{PermissionConstant.Role.GARDEN_LEADER.codeOf()}, areaId);
//
//        List<StaffDTO> staffList = ssoCallService.getStaffDTOList(ticket, TEACH_NURSERY_COURSEAUDIT_ACTION);
//
//        AreaSettingDTO nuserySetting = ssoCallService.getAreaSetting(CommonConstant.NURSERY_AUDITOR_OPEN_KEY, areaId);
//        AreaSettingDTO earlySetting = ssoCallService.getAreaSetting(CommonConstant.EARLY_AUDITOR_OPEN_KEY, areaId);
//        AreaSettingDTO areaBuildSetting = ssoCallService.getAreaSetting(CommonConstant.AREA_BUILD_OPEN_KEY, areaId);
//        // AreaSettingDTO kxThemeSetting = ssoCallService.getAreaSetting(CommonConstant.KXTHEME_AUDITOR_OPEN_KEY, areaId);
//        // AreaSettingDTO liveSetting = ssoCallService.getAreaSetting(CommonConstant.LIVE_AUDITOR_OPEN_KEY, areaId);
//        String auditNuseryTeacherId = nuserySetting==null?"":nuserySetting.getExtension();
//        String auditEarlyTeacherId = earlySetting==null?"":earlySetting.getExtension();
//        String auditAreaBuildTeacherId = earlySetting==null?"":areaBuildSetting.getExtension();
//        //String auditKXThemeTeacherId=kxThemeSetting.getExtension();
//        if(!env.equals("kxp") && !env.equals("kxd") ) {
//            //query.setCourseCategory(CourseConstant.Category.KX_THEME.codeOf());
//        }
//        for(StaffDTO staff : staffList){
//            if(staff.getId().equals(baseInfo.getId())){
//                Page<WebCoursePlanListVo> coursePlanList = courseAuditService.getCourseList(query);
//                return StatusResponse.success(coursePlanList);
//            }
//        }
//
//    	List<String> list = Lists.newArrayList();
//    	if(auditNuseryTeacherId.equals(baseInfo.getId())){
//    		list.add("1");
//    	}
//
//    	if(auditEarlyTeacherId.equals(baseInfo.getId())){
//    		list.add("2");
//    	}
//    	if(auditAreaBuildTeacherId.equals(baseInfo.getId())){
//    		list.add("3");
//    	}
//
//    	String join = String.join(",", list);
//    	if(StringUtils.isNotBlank(join)){
//    		query.setCourseCategorys(join);
//    	}
//    	Page<WebCoursePlanListVo> coursePlanList = courseAuditService.getCourseList(query);
//        return StatusResponse.success(coursePlanList);
//    }
//
//    @ApiOperation(value = "课程审核详情",httpMethod = "POST")
//    @RequestMapping("/course_detail")
//    public StatusResponse courseDetail(@RequestHeader(TICKET) String ticket, String coursePlanClassesId) {
//        String areaId = cache.getAreaId(ticket);
//        Map map = prepareCourseService.courseDetail(areaId, coursePlanClassesId);
//        return StatusResponse.success(map);
//    }
//
//    @ApiOperation(value = "审核课程",httpMethod = "POST")
//    @RequestMapping("/audit")
//    public StatusResponse audit(@RequestHeader(TICKET) String ticket, String coursePlanClassesId, Integer opinion) {
//        Boolean result = courseAuditService.audit(coursePlanClassesId, opinion);
//        return StatusResponse.success(result);
//    }
//
//    @ApiOperation(value = "获取需要审核的课程数量",httpMethod = "POST")
//    @RequestMapping("/non_audit_count")
//    public StatusResponse nonPrepareCount(@RequestHeader(TICKET) String ticket, String areaId) {
//        String staffId = cache.getStaff(ticket).getId();
//        Long count = courseAuditService.nonAuditCount(areaId, staffId);
//        return StatusResponse.success(count);
//    }

    //托班一日生活流程审核列表
    @ApiOperation(value = "托班审核一日生活分页",httpMethod = "POST")
    @RequestMapping(value = "/check_page")
    @RequiredPermissions(TEACH_NURSERY_COURSEAUDIT_ACTION)
    public StatusResponse checkPage(@RequestHeader String ticket,NurseryScheduleCheckQuery query) {
    	StaffDTO baseInfo = cache.getStaff(ticket);
    	String areaId = cache.getAreaId(ticket);

        List<StaffDTO> staffList = ssoCallService.getStaffDTOList(ticket, TEACH_NURSERY_COURSEAUDIT_ACTION);

        AreaSettingDTO liveSetting = ssoCallService.getAreaSetting(CommonConstant.LIVE_AUDITOR_OPEN_KEY, areaId);

        if(liveSetting != null && baseInfo.getId().equals(liveSetting.getExtension())){
            Page<NurseryScheduleCheckVo> page = nurseryScheduleService.checkPage(query);
            return StatusResponse.success(page);
        }

        for(StaffDTO staff:staffList){
            if(staff.getId().equals(baseInfo.getId())){
                Page<NurseryScheduleCheckVo> page = nurseryScheduleService.checkPage(query);
                return StatusResponse.success(page);
            }
        }
        //Map map = courseAuditService.getAuditor(areaId);
        return StatusResponse.success(new PageImpl<>(new ArrayList<>(), PageRequest.of(query.getPageNum(), query.getPageSize()), 0));
    }

    //托班一日生活流程审核
    @ApiOperation(value = "审核托班一日生活课程",httpMethod = "POST")
    @RequestMapping(value = "/check")
    public StatusResponse check(@RequestHeader String ticket, String checkId, Integer status) {
        return StatusResponse.success(nurseryScheduleService.check(ticket, checkId, status));
    }

}
